<template>
	<view class="content">
		<view class="form-block" style="border-radius:30rpx 30rpx 0 0 ;">
			<view class="form-title">基本信息</view>
			<view class="form-item">
				<view class="form-label">真实姓名</view>
				<view class="form-content">
					<input v-model="form.realName" maxlength="10" type="text" placeholder-class="inputPlaceholder"
						placeholder="请填写姓名" class="form-ctrl" />
					<view class="iconfont icon-gengduo1 arrow"></view>
				</view>
			</view>
			<!-- <view class="form-item">
				<view class="form-label">身份证号</view>
				<view class="form-content">
					<input v-model="form.idcardno" maxlength="18" type="idcard" placeholder-class="inputPlaceholder" placeholder="请填写身份证号" class="form-ctrl" />
					<view class="iconfont icon-gengduo1 arrow"></view>
				</view>
			</view> -->
		</view>
		<view class="form-block" style="border-radius:30rpx 30rpx 0 0 ;">
			<view class="form-title">联系方式</view>
			<view class="form-item">
				<view class="form-label">手机号码</view>
				<view class="form-content">
					<input v-model="form.phone" maxlength="11" type="number" placeholder-class="inputPlaceholder"
						placeholder="请填写手机号码" class="form-ctrl" disabled="true" />
					<view class="iconfont icon-gengduo1 arrow"></view>
				</view>
			</view>
			<view class="form-item" style="border-bottom: none;" v-if="label.length>0">
				<view class="form-label">培训种类</view>
				<view class="form-content">
					<picker :range="label" :value="labelIndex" @change="bindType" range-key="contentText"
						class="form-ctrl">
						{{label[labelIndex].contentText}}
					</picker>
					<view class="iconfont icon-gengduo1 arrow"></view>
				</view>
			</view>
			<!-- <view class="form-item">
				<view class="form-label">邮箱</view>
				<view class="form-content">
					<input v-model="form.eml" maxlength="30" type="" placeholder-class="inputPlaceholder" placeholder="选填" class="form-ctrl" />
					<view class="iconfont icon-gengduo1 arrow"></view>
				</view>
			</view>
			<view class="form-item">
				<view class="form-label">微信号</view>
				<view class="form-content">
					<input v-model="form.weChat" maxlength="30" type="text" placeholder-class="inputPlaceholder" placeholder="选填" class="form-ctrl" />
					<view class="iconfont icon-gengduo1 arrow"></view>
				</view>
			</view> -->
			<view class="btn" @click="handleSave">保存</view>
		</view>
	</view>
</template>

<script>
	import {
		validate
	} from '@/util/validate.js';
	import {
		groupTrainEnroll,
		findTrainListApi
	} from '@/api/train.js'
	import {
		getStorage
	} from '@/util/auth.js';
	import {
		mapGetters
	} from 'vuex';
	export default {
		data() {
			return {
				form: {
					realName: '',
					phone: '',
					label: ''
				},
				index: 0,
				label: [],
				labelIndex: 0,
				rules: {
					realName: {
						name: '真实姓名',
						required: true
					},
					phone: {
						name: '手机号',
						required: true,
						validator: function(value, form, callback) {
							if (!/^1[3-9][0-9]{9}$/.test(value)) {
								callback('请输入正确的手机号')
							}
							callback();
						}
					}
				},
				id: null,

			}
		},
		methods: {
			bindType(e) {
				this.labelIndex = e.detail.value
			},
			getList() {
				uni.showLoading({
					title: '加载中'
				})
				findTrainListApi({
					trainId: this.id
				}).then((res) => {
					this.label = res
				}).catch(() => {

				})
			},
			handleSave() {
				let form = JSON.parse(JSON.stringify(this.form));
				form.sid = this.id
				form.label = this.label[this.labelIndex].id
				if (validate(form, this.rules)) {
					uni.showLoading({
						title: '加载中',
						mask: true
					})
					groupTrainEnroll(form).then((res) => {
						uni.hideLoading()
						this.showToast(res, () => {
							uni.navigateBack()
						})
					}).catch((res) => {
						uni.hideLoading()
						this.showToast('提交失败');
					})
				}
			}
		},
		computed: {
			...mapGetters(['identity', 'user'])
		},
		onLoad(e) {
			this.id = e.id;
			this.form.phone = this.user.mobile;
			// if (e.type) {
			// 	this.label = e.type.split(',');
			// } else {
			// 	this.label = []
			// }
			this.getList()
		}
	}
</script>

<style lang="scss">
	page {
		background: #F9F9F9;
	}

	.content {
		width: 100%;

		.btn {
			width: 350rpx;
			height: 80rpx;
			display: flex;
			justify-content: center;
			align-items: center;
			background: #FF7626;
			color: #fff;
			font-size: 28rpx;
			border-radius: 40rpx;
			margin: 0 auto;
			margin-top: 30rpx;
		}

		.form-block {
			width: 100%;
			box-shadow: 0 0 10px 0 #efefef;
			background: #fff;
			padding: 30rpx;

			.form-title {
				font-size: 36rpx;
				font-weight: bold;
			}

			.form-item {
				display: flex;
				flex-direction: column;
				border-bottom: 1px solid #efefef;
				padding-top: 30rpx;

				.form-label {
					font-size: 28rpx;
					color: #999;
				}

				.form-content {
					display: flex;
					flex-direction: row;
					align-items: center;

					.form-ctrl {
						height: 100rpx;
						line-height: 100rpx;
						flex-grow: 1;
					}

					.arrow {
						flex-grow: 0;
						color: #999;
						font-size: 24rpx;
					}
				}
			}
		}
	}
</style>